package com.zimmur.app.mocolink.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zimmur.app.mocolink.dao.GoodsItemMapper;
import com.zimmur.app.mocolink.dao.ShoppingCartMapper;
import com.zimmur.app.mocolink.domain.model.GoodsItem;
import com.zimmur.app.mocolink.domain.model.ShoppingCart;
import com.zimmur.app.mocolink.domain.model.ShoppingCartExample;
import com.zimmur.app.mocolink.domain.util.DateUtils;
import com.zimmur.app.mocolink.domain.util.StringUtils;
import com.zimmur.app.mocolink.domain.vo.ShoppingCartVo;
import com.zimmur.app.mocolink.service.IShoppingCartService;

@Service
public class ShoppingCartServiceImpl implements IShoppingCartService {
	private static final Logger log = LoggerFactory.getLogger(ShoppingCartServiceImpl.class);
	@Autowired
	private ShoppingCartMapper shoppingCartMapper;
	@Autowired
	private GoodsItemMapper goodsItemMapper;
	
	@Transactional
	@Override
	public int addCart(String[] cartInfos, int userId) {
		for(String cartInfo:cartInfos){
			String[] info = cartInfo.split(",");
			if(info.length>=2){
				Integer goodsItemId = Integer.valueOf(info[0]);
				Integer quantity = Integer.valueOf(info[1]);
				GoodsItem goodsItem = goodsItemMapper.selectByPrimaryKey(goodsItemId);
				
				if(goodsItem!=null){
					ShoppingCart cart = new ShoppingCart();
					cart.setGoodsId(goodsItem.getGoodsId());
					cart.setGoodsItemId(goodsItemId);
					cart.setColorName(goodsItem.getColorName());
					cart.setSizeName(goodsItem.getSizeName());
					cart.setQuantity(quantity);
					cart.setUserId(userId);
					cart.setSkuCode(goodsItem.getSku());
					cart.setCreateTime(DateUtils.getNow());
					shoppingCartMapper.insertSelective(cart);
				}
			}
		}
		return 1;
	}
	
	@Transactional
	@Override
	public int updateCart(String[] cartInfos) {
		for(String cartInfo:cartInfos){
			String[] info = cartInfo.split(",");
			Integer cartId = Integer.valueOf(info[0]);
			Integer quantity = Integer.valueOf(info[1]);
			ShoppingCart cart = shoppingCartMapper.selectByPrimaryKey(cartId);
			if(cart!=null){
				cart.setQuantity(quantity);
				shoppingCartMapper.updateByPrimaryKey(cart);
			}
			
		}
		return 1;
	}

	@Override
	public List<ShoppingCartVo> selectUserCart(int userId) {
		List<ShoppingCartVo> cartVos = shoppingCartMapper.selectVoByUserId(userId);
		return cartVos;
	}
	public int cleanQuantity(List<Integer> cartIdList, int userId) {
		return shoppingCartMapper.updateCartQuantity(cartIdList, 0);
	}

	@Override
	public int deleteCartByUserId(String[] cartIdArray,String[] goodsIdArray ,int userId){
		int flag = 0;
		List<Integer> goodsIdList=new ArrayList<>();
		if(cartIdArray!=null &&cartIdArray.length>0){
			List<Integer> cartIdList=new ArrayList<>();
			for(String cartId:cartIdArray){
				cartIdList.add(Integer.valueOf(cartId));
			}
			flag=shoppingCartMapper.updateCartQuantity(cartIdList, 0);
			if(flag>0){
				goodsIdList = shoppingCartMapper.selectGoodsIdByQuantity(userId);
			}
		}
		if(goodsIdArray!=null && goodsIdArray.length>0){
			for(String goodsId:goodsIdArray){
				goodsIdList.add(Integer.valueOf(goodsId));
			}
		}
		if(goodsIdList.size()>0){
			flag+=deleteCartByGoodsId(goodsIdList, userId);
		}
		
		return flag;
	}
	@Override
	public int deleteCartByGoodsId(List<Integer> goodsIdList,Integer userId) {
		ShoppingCartExample example = new ShoppingCartExample();
		example.createCriteria().andGoodsIdIn(goodsIdList).andUserIdEqualTo(userId);
		return shoppingCartMapper.deleteByExample(example);
	}
	@Override
	public int addGroupGoods(int groupGoodsId, int userId) {
		int flag = 0;
		List<GoodsItem> goodsItems = goodsItemMapper.selectByGroupGoodsId(groupGoodsId, userId);
		if(goodsItems!=null && goodsItems.size()>0){
			for(GoodsItem goodsItem:goodsItems){
				ShoppingCart cart = new ShoppingCart();
				cart.setGoodsId(goodsItem.getGoodsId());
				cart.setGoodsItemId(goodsItem.getId());
				cart.setColorName(goodsItem.getColorName());
				cart.setSizeName(goodsItem.getSizeName());
				cart.setQuantity(1);
				cart.setUserId(userId);
				cart.setSkuCode(goodsItem.getSku());
				cart.setCreateTime(DateUtils.getNow());
				flag+=shoppingCartMapper.insertSelective(cart);
			}
		}
		return flag;
	}

	@Override
	public int checkByGoodsId(Integer goodsId, Integer userId) {
		ShoppingCartExample example = new ShoppingCartExample();
		example.createCriteria().andGoodsIdEqualTo(goodsId).andUserIdEqualTo(userId);
		int count = shoppingCartMapper.countByExample(example);
		return count;
	}

	@Override
	public List<ShoppingCartVo> selectByCartIds(String[] cartIds) {
		List<Integer> cartIdList = new ArrayList<>();
		for(String cartId:cartIds){
			cartIdList.add(Integer.valueOf(cartId));
		}
		if(cartIdList.size()>0){
			return shoppingCartMapper.selectVoByCartIds(cartIdList);
		}
		return null;
	}

	

}
